package com.homelock.service;

import com.homelock.common.sequence.SnowflakeFactory;
import com.homelock.entity.UcUserEmergency;
import com.homelock.entity.UcUserEmergencyExample;
import com.homelock.mapper.HlUserCommunityMapper;
import com.homelock.mapper.UcUserEmergencyMapper;
import com.homelock.service.check.LockUserCheckUtil;
import com.homelock.vo.UserEmergencyVo;
import com.pi.base.enumerate.record.RecordState;
import com.pi.community.service.CommunityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;


@Service
public class UserEmergencyService {

    private static final Logger logger = LoggerFactory.getLogger(UserEmergencyService.class);
    private UcUserEmergencyMapper ucUserEmergencyMapper;

    @Autowired
    public UserEmergencyService(
            UcUserEmergencyMapper ucUserEmergencyMapper){
        this.ucUserEmergencyMapper = ucUserEmergencyMapper;
    }

    /**
     * 当前用户添加或者更新一个紧急联系人
     * @param userId 当前登录用户id
     * @param contactName 紧急联系人电话
     * @param contactMobile 紧急联系人电话
     * @return
     */
    @Transactional
    public boolean addOrUpdate(Long userId,String contactName,String contactMobile){
        // 数据校验
        LockUserCheckUtil.checkUserEmergency(contactName,contactMobile);
        contactName = contactName.trim();
        // 查询该用户是否有紧急联系人 有的话就更新 没有就新增
        UcUserEmergency userEmergency = info(userId);
        boolean addFlag = true;
        Date current = new Date();
        if(userEmergency == null){
            userEmergency = new UcUserEmergency();
            userEmergency.setId(SnowflakeFactory.getDefaultWorker().nextId());
            userEmergency.setCreateDate(current);
            userEmergency.setState(RecordState.STATE_NORMAL.getState());
            userEmergency.setVersion(1L);
            userEmergency.setUserId(userId);
        }else {
            // 数据无需更新
            if(contactName.equals(userEmergency.getContactName()) && contactMobile.equals(userEmergency.getContactMobile())){
                return true;
            }
            addFlag = false;
        }
        userEmergency.setContactName(contactName);
        userEmergency.setContactMobile(contactMobile);
        userEmergency.setUpdateDate(current);
        if(addFlag){
            ucUserEmergencyMapper.insertSelective(userEmergency);
        }else {
            ucUserEmergencyMapper.updateByPrimaryKey(userEmergency);
        }
        return true;
    }

    public UserEmergencyVo voInfo(Long userId){
        UcUserEmergency userEmergency = info(userId);
        UserEmergencyVo vo = new UserEmergencyVo();
        if(userEmergency == null){
            return vo;
        }
        vo.setEmergencyId(userEmergency.getId());
        vo.setName(userEmergency.getContactName());
        vo.setPhone(userEmergency.getContactMobile());
        return vo;
    }


    /**
     * 查询某个用户的紧急联系人
     * @param userId
     * @return
     */
    public UcUserEmergency info(Long userId){
        UcUserEmergencyExample emergencyExample = new UcUserEmergencyExample();
        emergencyExample.createCriteria().andUserIdEqualTo(userId).andStateEqualTo(RecordState.STATE_NORMAL.getState());
        List<UcUserEmergency> list = ucUserEmergencyMapper.selectByExample(emergencyExample);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }
}
